package com.ruoyi.match.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.match.domain.vo.NoticeConfirmVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.match.domain.NoticeConfirm;
import com.ruoyi.match.service.INoticeConfirmService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 公告确认Controller
 * 
 * @author zhangrui
 * @date 2025-04-05
 */
@RestController
@RequestMapping("/match/confirm")
public class NoticeConfirmController extends BaseController
{
    @Autowired
    private INoticeConfirmService noticeConfirmService;

    /**
     * 查询公告确认列表
     */
    @PreAuthorize("@ss.hasPermi('match:confirm:list')")
    @GetMapping("/list")
    public TableDataInfo list(NoticeConfirm noticeConfirm)
    {
        startPage();
        List<NoticeConfirm> list = noticeConfirmService.selectNoticeConfirmList(noticeConfirm);
        return getDataTable(list);
    }

    /**
     * 导出公告确认列表
     */
    @PreAuthorize("@ss.hasPermi('match:confirm:export')")
    @Log(title = "公告确认", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NoticeConfirm noticeConfirm)
    {
        List<NoticeConfirm> list = noticeConfirmService.selectNoticeConfirmList(noticeConfirm);
        ExcelUtil<NoticeConfirm> util = new ExcelUtil<NoticeConfirm>(NoticeConfirm.class);
        util.exportExcel(response, list, "公告确认数据");
    }

    /**
     * 获取公告确认详细信息
     */
    @PreAuthorize("@ss.hasPermi('match:confirm:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable("noticeId") Long noticeId)
    {
        return success(noticeConfirmService.selectNoticeConfirmByNoticeId(noticeId));
    }

    /**
     * 新增公告确认
     */
//    @PreAuthorize("@ss.hasPermi('match:confirm:add')")
//    @Log(title = "公告确认", businessType = BusinessType.INSERT)
    @PostMapping("/add/{noticeId}")
    public AjaxResult add(@PathVariable("noticeId") Long noticeId)
    {
        NoticeConfirm noticeConfirm = new NoticeConfirm();
        noticeConfirm.setNoticeId(noticeId);
        // 获取当前用户id、Name,设定为确认者
        Long userId = SecurityUtils.getUserId();
        noticeConfirm.setUserId(userId);
        noticeConfirm.setUserName(SecurityUtils.getUsername());

        return toAjax(noticeConfirmService.insertNoticeConfirm(noticeConfirm));
    }

    /**
     * 修改公告确认
     */
    @PreAuthorize("@ss.hasPermi('match:confirm:edit')")
    @Log(title = "公告确认", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NoticeConfirm noticeConfirm)
    {
        return toAjax(noticeConfirmService.updateNoticeConfirm(noticeConfirm));
    }

    /**
     * 删除公告确认
     */
    @PreAuthorize("@ss.hasPermi('match:confirm:remove')")
    @Log(title = "公告确认", businessType = BusinessType.DELETE)
	@DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds)
    {
        return toAjax(noticeConfirmService.deleteNoticeConfirmByNoticeIds(noticeIds));
    }


    /**
     * 查询当前用户已经确认过的公告。
     */
    @PostMapping("/myConfirm/list")
    public AjaxResult getConfirmedNotices(@RequestBody List<Long> noticeIds) {
        System.out.println("noticeIds:"+noticeIds);
        Long userId = SecurityUtils.getUserId();
        List<Long> confirmedIds = noticeConfirmService.findConfirmedNotices(noticeIds, userId);
        System.out.println("confirmedIds:"+confirmedIds);
        return AjaxResult.success(confirmedIds);
    }


    @PostMapping("/confirmedNameList/{noticeId}")
    public AjaxResult getConfirmedNames(@PathVariable("noticeId") Long noticeId) {
        NoticeConfirmVo noticeConfirmVo = noticeConfirmService.selectConfirmedNames(noticeId);
        return AjaxResult.success(noticeConfirmVo);
    }



}
